package com.cff.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cff.dao.ScourtDao;
import com.cff.dao.ScourtInfoDao;
import com.cff.dao.UserDao;
import com.cff.domain.Scourt;
import com.cff.domain.ScourtInfo;
import com.cff.domain.User;
import com.cff.dto.RetCode;
import com.cff.util.DateUtil;

@RestController
@RequestMapping("/admin")
public class AdminRest {

	@Autowired
	ScourtDao scourtDao;
	
	@Autowired
	ScourtInfoDao scourtInfoDao;

	@Autowired
	UserDao userDao;

	@RequestMapping(value = "/bookList", method = RequestMethod.POST, produces = "application/json")
	public List<Scourt> bookList(@RequestParam String bookTime, @RequestParam String courtName) {
		List<Scourt> list = null;
		if (bookTime == null || bookTime.trim().length() < 1) {
			if (courtName == null || courtName.trim().length() < 1) {
				list = (List<Scourt>) scourtDao.findAll();
			} else {
				list = scourtDao.findByCourtName(courtName);
			}
		} else {
			if (courtName == null || courtName.trim().length() < 1) {
				list = scourtDao.findByBdate(bookTime);
			} else {
				list = scourtDao.findByCourtNameAndBdate(courtName, bookTime);
			}

		}
		for(Scourt scourt : list){
			scourt.setStartTime(scourt.getBdate() + " " +  scourt.getStartHour());
			scourt.setEndTime(scourt.getBdate() + " " +  scourt.getEndHour());
		}
		return list;
	}
	
	@RequestMapping(value = "/addCourt", method = RequestMethod.POST, produces = "application/json")
	public RetCode addCourt(@RequestBody ScourtInfo scourtInfo) {
		RetCode retCode = new RetCode();
		if(scourtInfo == null){
			retCode.setErrorCode("400");
			retCode.setErrorMsg("数据为空！");
			return retCode;
		}
		try {
			scourtInfo.setCreateDate(DateUtil.format(new Date(), DateUtil.SimpleDatePattern));
			scourtInfoDao.save(scourtInfo);
			retCode.setErrorCode("200");
			retCode.setErrorMsg("场地添加成功！");
		} catch (Exception e) {
			retCode.setErrorCode("500");
			retCode.setErrorMsg("场地添加失败！");
		}

		return retCode;
	}

	@RequestMapping(value = "/deleteBook", method = RequestMethod.POST, produces = "application/json")
	public RetCode deleteBook(@RequestParam int id) {
		RetCode retCode = new RetCode();
		try {
			scourtDao.delete(id);
			retCode.setErrorCode("200");
			retCode.setErrorMsg("删除成功！");
		} catch (Exception e) {
			retCode.setErrorCode("500");
			retCode.setErrorMsg("删除失败！");
		}

		return retCode;
	}
	
	@RequestMapping(value = "/deleteCourt", method = RequestMethod.POST, produces = "application/json")
	public RetCode deleteCourt(@RequestParam int courtId) {
		RetCode retCode = new RetCode();
		try {
			scourtInfoDao.delete(courtId);
			retCode.setErrorCode("200");
			retCode.setErrorMsg("删除成功！");
		} catch (Exception e) {
			retCode.setErrorCode("500");
			retCode.setErrorMsg("删除失败！");
		}

		return retCode;
	}

	@RequestMapping(value = "/users", method = RequestMethod.POST, produces = "application/json")
	public List<User> users(@RequestParam String userName) {
		List<User> list = null;
		if (userName == null || userName.trim().length() < 1) {
			list = (List<User>) userDao.findAll();
		} else {
			list = new ArrayList<>();
			list.add(userDao.findByUserName(userName));
		}
		return list;
	}
	
	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST, produces = "application/json")
	public RetCode deleteUser(@RequestParam String userName) {
		RetCode retCode = new RetCode();
		try {
			userDao.delete(userName);
			retCode.setErrorCode("200");
			retCode.setErrorMsg("删除成功！");
		} catch (Exception e) {
			retCode.setErrorCode("500");
			retCode.setErrorMsg("删除失败！");
		}

		return retCode;
	}
}
